package com.jade.explorer.ui.imp;

import java.io.File;
import java.io.FileFilter;
import java.util.ArrayList;
import java.util.HashMap;

import org.eclipse.jface.viewers.ListViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.DirectoryDialog;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeItem;

import com.jade.explorer.XMLpart;
import com.jade.explorer.dirscan.FileSystem;
import com.jade.explorer.views.JadeSharedImages;
import com.jade.logger.Logger;

public class ImportProjectHard implements IImportTarget {
	
	private String _title = "Project";
	private String _localPath = "";
	private boolean _result = false;
	private Composite _layer = null;
	private Button _b = null;
	private HashMap<String, String> _road = new HashMap<String, String>();
	private ArrayList<String> _finalList = new ArrayList<String>();
	private ArrayList<String> _nameList = new ArrayList<String>();
	private String _impPath = "";
	private HashMap<String, ArrayList<File>> _data = new HashMap<String, ArrayList<File>>();
	private boolean _over = false;
	private boolean _add = false;

	@Override
	public void addTargetItem(Tree t) {
		TreeItem ti = new TreeItem(t, 0);
		ti.setText(_title);
		ti.setImage(JadeSharedImages.getImage(JadeSharedImages.IMG_FOLDER_STAND));
	}

	@Override
	public void setLocalPath(String localPath) {
		_localPath = localPath;
	}

	@Override
	public boolean getResult() {
		return _result;
	}

	@Override
	public void setVisible(boolean b) {
		_layer.setVisible(b);
	}

	@Override
	public void setEnabled(boolean b) {
		_layer.setVisible(b);
	}

	@Override
	public String getTitle() {
		return _title;
	}

	@Override
	public Composite createLayer(Composite parent) {
		
		_layer = new Composite(parent, SWT.NONE);
		_layer.setBounds(11, 13, 434, 317);
		_layer.setBounds(0, 44, 444, 327);
		
		final Text pathText = new Text(_layer, SWT.BORDER);
		pathText.setEditable(false);
		pathText.setBounds(10, 114, 251, 21);
		
		final Shell shell = parent.getShell();
		Button pathButton = new Button(_layer, SWT.NONE);
		pathButton.setBounds(267, 112, 167, 25);
		pathButton.setText("Choose path");
		pathButton.addSelectionListener(new SelectionAdapter(){
			
			public void widgetSelected(SelectionEvent e){
				DirectoryDialog dialog = new DirectoryDialog(shell);
				String exportPath = dialog.open(); 
				
				if (exportPath != null){
					pathText.setText(exportPath);
					_impPath = exportPath;
					_b.setEnabled(true);
				}
			}
		});
		
		final Button overwriteButton = new Button(_layer, SWT.CHECK);
		overwriteButton.setBounds(10, 140, 424, 16);
		overwriteButton.setText("Overwrite existing files");
		overwriteButton.setSelection(false);
		
		final Button addButton = new Button(_layer, SWT.CHECK);
		addButton.setBounds(10, 160, 424, 16);
		addButton.setText("Add different files if project exists");
		addButton.setSelection(false);
		
		overwriteButton.addSelectionListener(new SelectionAdapter(){
			
			public void widgetSelected(SelectionEvent e){
				
				if (_over){
					_over  = false;
					addButton.setEnabled(true);
				}
				else{
					_over = true;
					addButton.setEnabled(false);
				}
				
			}
		});
		
		addButton.addSelectionListener(new SelectionAdapter(){
			
			public void widgetSelected(SelectionEvent e){
				
				if (_add) {
					_add  = false;
					overwriteButton.setEnabled(true);
				}
				else{
					_add = true;
					overwriteButton.setEnabled(false);
				}
			}
		});
		
		return _layer;
	}

	@Override
	public void buttonControl(Button b) {
		_b = b;
	}

	@Override
	public boolean ready() {
		if (_impPath != "")
			return true;
		return false;
	}

	@Override
	public void run() {
		
		
		if (ready()){
			if(XMLpart.checkProjectStucture(_impPath + "\\.project")){				
				File imp = new File(_impPath);
				File ch = new File(_localPath + "\\" + imp.getName());
				
				if(ch.exists() && _over && !_add){
					_result = FileSystem.copyFilderWithOverwritingExistingFiles(_impPath, _localPath);
					parseProject(ch);
				}
				else if(ch.exists() && !_over && _add){
					_result = FileSystem.copyFilderWithNoOverwritingExistingFiles(_impPath, _localPath);
					parseProject(ch);
				}
				else if (ch.exists() && !_over && !_add){
					Logger.print(Logger.STATE_WARNING, "Can not import project, as similar one already exists. Turn on file overwrite or add different in import dialog.");
				}
				else if (!ch.exists()){
					_result = FileSystem.copyFilderWithOverwritingExistingFiles(_impPath, _localPath);
				}
			}
		}
	}

	@Override
	public HashMap<String, ArrayList<File>> getImportedData() {
		return _data;
	}
	

	@Override
	public void clearData() {
		_road.clear();
		_finalList.clear();
		_nameList.clear();
		_data.clear();
	}

	
	public void parseProject(File wh){
		
		ArrayList<File> proj = new ArrayList<File>();
		proj.add(wh);
		
		_data.put("Project", proj);
		
		ArrayList<File> parents = new ArrayList<File>();
		
		for (File f : wh.listFiles()){
			
			if(f.getName().toLowerCase().equals("objects")){
				ArrayList<File> objs = new ArrayList<File>();
				for (File obj : f.listFiles()){
					objs.add(obj);
				}
				parents.add(f);
				if (objs.size() > 0);
					_data.put("Object", objs);
				
			}
			else if(f.getName().toLowerCase().equals("anisotropy")){
				ArrayList<File> anis = new ArrayList<File>();
				for (File ani : f.listFiles()){
					anis.add(ani);
				}
				parents.add(f);
				if (anis.size() > 0);
					_data.put("Anisotropy", anis);
				
			}
			else if(f.getName().toLowerCase().equals("placement")){
				ArrayList<File> doms = new ArrayList<File>();
				for (File dom : f.listFiles()){
					doms.add(dom);
				}
				parents.add(f);
				if (doms.size() > 0);
					_data.put("Domain", doms);
				
			}
			else if(f.getName().toLowerCase().equals("pictures")){
				ArrayList<File> pics = new ArrayList<File>();
				for (File pic : f.listFiles()){
					pics.add(pic);
				}
				parents.add(f);
				if (pics.size() > 0);
					_data.put("Picture", pics);
					
			}
		}
		
		if (parents.size() > 0)
			_data.put("Parent", parents);
		
	}
	
		
	
}
